//
//  HomeBaseViewController.m
//  CarLifes
//
//  Created by yu on 2017/6/27.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "HomeBaseViewController.h"
#import "LocationAnnotationView.h"

@interface HomeBaseViewController ()
{
    // 自定义UserLocation标注点
    LocationAnnotationView *_locationAnnotationView;
    
}

@end

@implementation HomeBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
        _isDarkMode = self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark;
    } else {
        _isDarkMode = NO;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
    if (@available(iOS 13.0, *)) {
        if (previousTraitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
            _isDarkMode = YES;
            // 只有当前地图是普通模式，才变为夜间模式
            if (_mapView.mapType == MAMapTypeStandard) {
                _mapView.mapType = MAMapTypeStandardNight;
            }
        } else {
            _isDarkMode = NO;
            // 只有当前地图是夜间模式，才变为标准模式
            if (_mapView.mapType == MAMapTypeStandardNight) {
                _mapView.mapType = MAMapTypeStandard;
            }
        }
    } else {
        PPLog(@"***77777");
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //_mapView.showTraffic = YES;
}

- (void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated
{
    if (mode == MAUserTrackingModeNone)
    {
        [self.userLocBtn setImage:IMAGE_NAMED(@"location_no") forState:UIControlStateNormal];
    }
    else
    {
        [self.userLocBtn setImage:IMAGE_NAMED(@"location_yes") forState:UIControlStateNormal];
    }
}

- (void)initMapView
{
    self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.mapView];
    
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MAUserTrackingModeFollow;
    self.mapView.showsCompass = NO;
    self.mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
    
    self.mapView.mapType = self.isDarkMode ? MAMapTypeStandardNight : MAMapTypeStandard;
}


#pragma mark - button action -
#pragma mark - 定位 -
- (void)userLocation:(UIButton *)sender
{
    if (self.mapView.userTrackingMode == MAUserTrackingModeNone)
    {
        [self.mapView setUserTrackingMode:MAUserTrackingModeFollow];
    }
}

#pragma mark - 缩放 -
- (void)magnify:(UIButton *)sender
{
    int zoom = [_mapView getMapStatus].zoomLevel;
    zoom += 1;

    if (zoom == _mapView.maxZoomLevel)
    {
        self.shrinkBtn.enabled = YES;
        self.magnifyBtn.enabled = NO;
    }
    else
    {
        self.shrinkBtn.enabled = YES;
    }
    
    [_mapView setZoomLevel:zoom atPivot:CGPointMake(_mapView.centerX_sd, _mapView.centerY_sd) animated:YES];
}

- (void)shrink:(UIButton *)sender
{
    int zoom = [_mapView getMapStatus].zoomLevel;
    zoom -= 1;

    if (zoom == _mapView.minZoomLevel)
    {
        self.shrinkBtn.enabled = NO;
        self.magnifyBtn.enabled = YES;
    }
    else
    {
        self.magnifyBtn.enabled = YES;
    }
    
    [_mapView setZoomLevel:zoom atPivot:CGPointMake(_mapView.centerX_sd, _mapView.centerY_sd) animated:YES];
}

#pragma mark - 路况 -
- (void)roadCondition:(id)sender
{
//    _roadConditionBtn.selected = !_roadConditionBtn.selected;
//    _mapView.showTraffic = _roadConditionBtn.selected ? YES : NO;
}

#pragma mark - 切换地图图层 -
- (void)switcher:(id)sender
{
    _switcherBtn.selected = !_switcherBtn.selected;
    
//    if (_switcherBtn.isSelected) {
//        if (_isDarkMode) {
//            _mapView.mapType = MAMapTypeSatellite;
//        } else {
//            _mapView.mapType = MAMapTypeStandardNight;
//        }
//    }
    
    
    if (_isDarkMode) {
        if (_switcherBtn.isSelected) {
            _mapView.mapType = MAMapTypeSatellite;
        } else {
            _mapView.mapType = MAMapTypeStandardNight;
        }
    } else {
        if (_switcherBtn.isSelected) {
            _mapView.mapType = MAMapTypeSatellite;
        } else {
            _mapView.mapType = MAMapTypeStandard;
        }
    }
    
    //_mapView.mapType = _switcherBtn.selected ? MAMapTypeSatellite : MAMapTypeStandard;
}

- (void)setupButton
{
    UIButton *userLocBtn = [[UIButton alloc] init];
    self.userLocBtn = userLocBtn;
    [userLocBtn setImage:IMAGE_NAMED(@"location_yes") forState:UIControlStateNormal];
    [userLocBtn addTarget:self action:@selector(userLocation:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *zoomBtnContainer = [[UIView alloc] init];
    zoomBtnContainer.backgroundColor = [UIColor clearColor];
    self.zoomBtnContainer = zoomBtnContainer;
    {
        UIButton *magnifyBtn = [[UIButton alloc] init];
        self.magnifyBtn = magnifyBtn;
        [magnifyBtn setImage:IMAGE_NAMED(@"plus") forState:UIControlStateNormal];
        [magnifyBtn addTarget:self action:@selector(magnify:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *shrinkBtn = [[UIButton alloc] init];
        self.shrinkBtn = shrinkBtn;
        [shrinkBtn setImage:IMAGE_NAMED(@"minus") forState:UIControlStateNormal];
        [shrinkBtn addTarget:self action:@selector(shrink:) forControlEvents:UIControlEventTouchUpInside];
        
        [zoomBtnContainer sd_addSubviews:@[magnifyBtn,shrinkBtn]];
        
        magnifyBtn.sd_layout
        .rightSpaceToView(zoomBtnContainer, 0)
        .topSpaceToView(zoomBtnContainer, 0)
        .leftSpaceToView(zoomBtnContainer, 0)
        .widthIs(45)
        .heightEqualToWidth();
//        magnifyBtn.backgroundColor = [UIColor redColor];
        
        shrinkBtn.sd_layout
        .leftEqualToView(magnifyBtn)
        .topSpaceToView(magnifyBtn, -5)
        .widthRatioToView(magnifyBtn, 1)
        .heightRatioToView(magnifyBtn, 1);
//        shrinkBtn.backgroundColor = [UIColor blueColor];
        
        [zoomBtnContainer setupAutoHeightWithBottomView:shrinkBtn bottomMargin:0];
    }
    
    UIView *otherBtnContainer = [[UIView alloc] init];
    otherBtnContainer.backgroundColor = [UIColor clearColor];
    self.otherBtnContainer = otherBtnContainer;
    {
        // 路况
//        UIButton *roadConditionBtn = [[UIButton alloc] init];
//        self.roadConditionBtn = roadConditionBtn;
//        roadConditionBtn.selected = YES;
//        [roadConditionBtn setImage:IMAGE_NAMED(@"road_condition_normal") forState:UIControlStateNormal];
//        [roadConditionBtn setImage:IMAGE_NAMED(@"road_condition_select") forState:UIControlStateSelected];
//        [roadConditionBtn addTarget:self action:@selector(roadCondition:) forControlEvents:UIControlEventTouchUpInside];
        
        // 标准地图、卫星地图切换
        UIButton *switcherBtn = [[UIButton alloc] init];
        self.switcherBtn = switcherBtn;
        [switcherBtn setImage:IMAGE_NAMED(@"standard_map") forState:UIControlStateNormal];
        [switcherBtn setImage:IMAGE_NAMED(@"statelite_map") forState:UIControlStateSelected];
        [switcherBtn addTarget:self action:@selector(switcher:) forControlEvents:UIControlEventTouchUpInside];
        
        [otherBtnContainer sd_addSubviews:@[switcherBtn]];

//        roadConditionBtn.sd_layout
//        .rightSpaceToView(otherBtnContainer, 0)
//        .topSpaceToView(otherBtnContainer, 0)
//        .leftSpaceToView(otherBtnContainer, 0)
//        .heightEqualToWidth();
        
        switcherBtn.sd_layout
        .topSpaceToView(otherBtnContainer, 0)
        .widthRatioToView(otherBtnContainer, 1)
        .heightRatioToView(otherBtnContainer, 1);
        
        [otherBtnContainer setupAutoHeightWithBottomView:switcherBtn bottomMargin:0];
    }
    
    [self.view sd_addSubviews:@[userLocBtn,zoomBtnContainer,otherBtnContainer]];
    
    userLocBtn.sd_layout
    .leftSpaceToView(self.view, 5)
    .bottomSpaceToView(self.view, 15)
    .widthIs(45)
    .heightEqualToWidth();
    
    zoomBtnContainer.sd_layout
    .widthIs(45)
    .rightSpaceToView(self.view, 5)
    .bottomSpaceToView(self.view, 15);

    otherBtnContainer.sd_layout
    .widthIs(45)
    .heightEqualToWidth()
    .rightSpaceToView(self.view, 5)
    .bottomSpaceToView(zoomBtnContainer, 20);
    
}

#pragma mark - MAMapViewDelegate -
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
    /* 自定义userLocation对应的annotationView */
    if ([annotation isKindOfClass:[MAUserLocation class]])
    {
        static NSString *userLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[LocationAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:userLocationStyleReuseIndetifier];
            
            annotationView.canShowCallout = YES;
        }
        
        _locationAnnotationView = (LocationAnnotationView *)annotationView;
        [_locationAnnotationView updateImage:[UIImage imageNamed:@"userPosition"]];
        
        return annotationView;
    }
    
    return nil;
}

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    /* 自定义定位精度对应的MACircleView */
    if (overlay == mapView.userLocationAccuracyCircle)
    {
        MACircleRenderer *accuracyCircleRenderer = [[MACircleRenderer alloc] initWithCircle:(MACircle *)overlay];
        
        accuracyCircleRenderer.strokeColor  = kMAOverlayRendererDefaultStrokeColor;
        accuracyCircleRenderer.fillColor    = [kBaseColor colorWithAlphaComponent:0.1];
        
        return accuracyCircleRenderer;
    }
    
    return nil;
}

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (!updatingLocation && _locationAnnotationView != nil)
    {
        _locationAnnotationView.rotateDegree = userLocation.heading.trueHeading - self.mapView.rotationDegree;
    }
}


@end
